package com.qingcloud.adminbackend.command;

import cn.hutool.core.util.StrUtil;
import com.qingcloud.adminbackend.exception.WmCommonException;
import com.qingcloud.df.sdk.exchange.enums.ProtocolContentEnum;
import com.qingcloud.df.sdk.exchange.model.ProtocolFieldModel;
import com.qingcloud.df.sdk.exchange.parser.ProtocolFieldProcessor;
import com.qingcloud.df.sdk.exchange.protocol.ICommandProtocol;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 源码类型指令加载器
 *
 * @author wangjinnan
 */
@Slf4j
public class SourceCommandLoader {

    /**
     * 源码实现的对象
     */
    private ICommandProtocol command;

    /**
     * 源码配置提取器构造方法
     *
     * @param sourceCode 组件源码
     * @throws Exception 编译出现异常
     */
    public SourceCommandLoader(Integer id, String projectName, String sourceCode, String pom) {
        final Object instance = ComponentInstanceContainer.getInstance(id);
        if (instance instanceof ICommandProtocol) {
            this.command = (ICommandProtocol) instance;
            return;
        }

        //获取当前组件工作根目录
//        String workDir = MavenTools.getComponentWorkDir(id, projectName);
        //下载组件依赖
        List<String> dependencies = Collections.emptyList();
//        if (StrUtil.isNotBlank(pom)) {
//            dependencies = MavenTools.downloadDependencies(pom, workDir);
//        }

        //创建类加载器
        try {
            //加载类
            Object instanceObject = ComponentInstanceContainer.buildSourceInstance(id, sourceCode, dependencies, ICommandProtocol.class);
            if (Objects.isNull(instanceObject)) {
                log.warn("load component {}-{} result is null", projectName, id);
                return;
            }
            this.command = (ICommandProtocol) instanceObject;
            //执行配置参数和输出的方法
        } catch (Throwable e) {
            log.error(e.getMessage(), e);
            throw new WmCommonException("加载指令类错误," + e.getMessage());
        }

    }

    /**
     * 解析指令类字段信息
     */
    public List<ProtocolFieldModel> parseField() {
        if (Objects.nonNull(this.command)) {
            return ProtocolFieldProcessor.getInstance().parse(this.command);
        }
        return Collections.emptyList();
    }

    /**
     * 传输内容类型
     */
    public String getContentType() {
        if (Objects.nonNull(this.command)) {
            if (this.command.getContentType() == ProtocolContentEnum.ASCII) {
                return "ASCII编码字符串";
            }
            if (this.command.getContentType() == ProtocolContentEnum.UTF8) {
                return "UTF8编码字符串";
            }
            if (this.command.getContentType() == ProtocolContentEnum.HEX) {
                return "二进制字节码";
            }
        }
        return "";
    }

    /**
     * 传输内容格式
     */
    public String getContentFormat() {
        if (Objects.nonNull(this.command)) {
            final ProtocolContentEnum contentType = this.command.getContentType();
            if (ProtocolContentEnum.ASCII == contentType) {
                return "ISO8859-1编码字符串";
            }
            if (ProtocolContentEnum.UTF8 == contentType) {
                return "UTF8编码字符串";
            }
            if (ProtocolContentEnum.HEX == contentType) {
                return "字节16进制编码字符串";
            }
        }
        return "";
    }

    /**
     * 设置指令对象参数值
     */
    public void setCommandFieldValue(String fieldName, String value) {
        ProtocolFieldProcessor.getInstance().setFieldValue(fieldName, value, this.command);
    }

    /**
     * 获取指令内容
     */
    public String getContent() {
        this.command.calculate();
        return this.command.getContent();
    }

    /**
     * 获取指令序列号
     */
    public String getFormat() {
        return this.command.getContentType().name().toLowerCase();
    }

    /**
     * 获取指令序列号
     */
    public String getSerialNumber() {
        this.command.calculate();
        return this.command.getSerialNumber();
    }

}
